package connectordemo;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏李涛
 * Date: 2024-07-07
 * Time: 12:56
 */
public class Student implements Comparable<Student> {
    public int age;
    public String name;

    public Student(int age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        //String类型也实现了Comparable接口
        return this.name.compareTo(o.name);

//        return this.age - o.age;
    }

    public static void my_Sort(Comparable[] comparables) {
        for (int i = 0; i < comparables.length-1; i++) {
            for (int j = 0; j < comparables.length-1-i; j++) {
                if (comparables[j].compareTo(comparables[j+1]) > 0) {
                    Comparable tmp = comparables[j];
                    comparables[j] = comparables[j+1];
                    comparables[j+1] = tmp;
                }
            }
        }
    }

    public static void main(String[] args) {
        Student[] student = new Student[4];
        student[0] = new Student(18, "wada");
        student[1] = new Student(17, "dawa");
        student[2] = new Student(16, "hhh");
        student[3] = new Student(15, "xxx");

        System.out.println("排序前" + Arrays.toString(student));

        Student.my_Sort(student);

        System.out.println("排序后" + Arrays.toString(student));
    }
}
